<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/13
 * Time: 20:26
 */

class Color_model extends CI_Model
{

    public $table_name = 'colors';

    public $code_setting = [
        '01' => '黑',
        '02' => '白',
        '03' => '蓝',
        '04' => '绿',
        '05' => '黄',
        '06' => '橙',
        '07' => '红',
        '08' => '棕',
        '09' => '紫',
        '10' => '灰',
        '11' => '粉'
    ];
    
    public $limit_rows = 15;
    
    public function color_option()
    {

        $query = $this->db->from('colors')->where("is_delete = 0")->order_by('color_en ASC')->get();

        $option = array();
        $color = array();
        foreach ($query->result_array() as $row) {
            $option[$row['color_en']] = "{$row['color_en']}({$row['color_zh']})";
            $color[$row['color_en']] = $row;
        }

        return array('option' => $option, 'color' => $color);
    }

    public function get_color_num($color_name)
    {
        $query = $this->db->from('colors')->where("color_en = '$color_name'")->get();
        $color = $query->row_array();

        if (!$color) {
            $color['color_num'] = $this->insert_color(array(
                'color_en' => $color_name,
                'color_zh' => ''
            ));
        }

        return $color['color_num'];
    }
    
    public function _where($param = array())
    {
        if ($param['color_name']) {
            $this->db->where("(color_en LIKE '%{$param['color_name']}%' OR color_zh LIKE '%{$param['color_name']}%')");
        }
    }
    
    public function color_list($param = array())
    {
        $this->db->from('colors')
            ->limit($this->limit_rows,$this->limit_rows * $param['page'])
            ->order_by('color_id desc');
        
        $this->_where($param);
        
        $query = $this->db->get();
        
        $color = $query->result_array();
        
        return $color;
    }
    
    public function count($param = array())
    {
        $this->db->from('colors');
    
        $this->_where($param);
        
        return $this->db->count_all_results();
    }
    
    public function color_info($param = array())
    {
        $query = $this->db->from('colors')->where($param)->get();
        
        return $query->row_array();
    }
    
    public function insert_color($data = array(), $id = 0)
    {

        $color_data = array(
            'color_en' => $data['color_en'],
            'color_zh' => $data['color_zh'],
            'new_color_code' => $data['new_color_code'],
            'color_code' => '',
            'is_delete' => $data['is_delete'],
            'color_rgb' => '',
            'option_id' => $data['option_id']
        );

        if ($id) {

            $color_info = $this->color_info(array('color_id' => $id));

            if ($color_info['color_on'] <= 0 || $data['option_id'] != $color_info['option_id']) {
                $color_data['color_on'] = $this->max_color_num($data['option_id']);
            }

            $this->db->where('color_id', $id);
            $this->db->update('colors', $color_data);
        } else {
            $color_data['color_on'] = $this->max_color_num($data['option_id']);

            $this->db->insert('colors', $color_data);
        }

        return $color_data['color_num'];
    }
    
    public function max_color_num($option_id)
    {
        $query = $this->db->from('colors')
            ->select('color_on')
            ->where("option_id = '{$option_id}'")
            ->order_by('color_on DESC')
            ->get();
        
        $color = $query->row_array();
        
        return ltrim($color['color_on'], '0') + 1;
    }
}
